<template>
  <div>
    <el-form ref="ruleForm" v-loading="loading" :model="blog" label-width="100px">
      <el-form-item label="博客标题" prop="blogTitle">
        <el-input v-model="blog.blogTitle" />
      </el-form-item>
      <el-form-item label="封面" prop="blogImage">
        <!--      <el-input v-model="blog.blogImage" />-->
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :headers="headers"
          :show-file-list="false"
          :on-success="handleUploadSuccess"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>
      <el-form-item label="分类名称" prop="typeName">
        <el-select v-model="blog.blogType" filterable clearable placeholder="请选择分类" style="width: 100%">
          <el-option
            v-for="item in typeList"
            :key="item.typeId"
            :label="item.typeName"
            :value="item.typeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标签" prop="tagIds">
        <el-select v-model="tagIds" multiple placeholder="请选择标签">
          <el-option
            v-for="item in tagList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            @focus="getEnabledTags"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="博客内容" prop="typeName">
        <!--      <el-input v-model="blog.blogContent" />-->
        <Tinymce v-model="blog.blogContent" />
      </el-form-item>
      <el-form-item label="来源" prop="blogSource">
        <el-input v-model="blog.blogSource" />
      </el-form-item>
      <el-form-item label="博客简介" prop="blogRemark">
        <el-input v-model="blog.blogRemark" type="textarea" />
      </el-form-item>
      <el-form-item label="提交" label-width="100px">
        <el-button type="primary" @click="submitForm">提交</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import blogApi from '../../../api/blog'
import Tinymce from '@/components/Tinymce'
import { getToken } from '@/utils/auth'
import tagApi from '../../../api/tag'
export default {
  name: 'BlogAdd',
  components: {
    Tinymce
  },
  data() {
    return {
      blog: {
        blogTitle: '',
        blogImage: null,
        blogContent: '',
        blogType: '',
        blogRemark: null,
        blogSource: ''
      },
      headers: {
        Authorization: getToken()
      },
      uploadUrl: process.env.VUE_APP_UPLOAD_URL,
      imageUrl: '',
      tagIds: [],
      tagList: [],
      typeList: this.$store.getters.typeList,
      loading: false
    }
  },
  methods: {
    handleUploadSuccess(res, file) {
      this.imageUrl = res.data
      this.blog.blogImage = res.data
    },
    getUploadUrl() {
      var vueappuploadurl = process.env.VUE_APP_UPLOAD_URL
      return vueappuploadurl
    },
    getEnabledTags() {
      tagApi.getEnabledTags().then(res => {
        if (res.code === 200) {
          this.tagList = res.data
        }
      })
    },
    submitForm() {
      this.loading = true
      blogApi.save(this.blog).then(res => {
        if (res.code === 200) {
          this.loading = false
          this.$emit('closeBlogAddDialog')
          this.$emit('getBlogList')
          this.$message.success(res.msg)
        } else {
          this.loading = false
        }
      })
    }
  }
}
</script>

<style scoped>
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
